Page({
  data: {
    banners: [
      { id: 1, image: 'https://dummyimage.com/1200x600/eeeeee/aaaaaa.png&text=banner' },
      { id: 2, image: 'https://dummyimage.com/1200x600/eeeeee/aaaaaa.png&text=banner' }
    ],
    hotActivities: [
      { id: 1, title: '春日海边徒步', signup: 56, image: 'https://dummyimage.com/600x400/eeeeee/aaaaaa.png&text=activity' },
      { id: 2, title: '摄影分享会', signup: 32, image: 'https://dummyimage.com/600x400/eeeeee/aaaaaa.png&text=activity' },
      { id: 3, title: '广场舞交流', signup: 41, image: 'https://dummyimage.com/600x400/eeeeee/aaaaaa.png&text=activity' }
    ],
    currentTab: 0,
    posts: [
      {
        id: 1,
        user: { name: '茶如一家', avatar: 'https://dummyimage.com/120x120/cccccc/888888.png&text=U' },
        time: '1月3日',
        title: '今天阳光正好',
        content: '公园散步时偶遇一位老友，聊起往事，分享生活点滴，倍感温暖。',
        images: [
          'https://dummyimage.com/600x400/eeeeee/aaaaaa.png&text=1',
          'https://dummyimage.com/300x300/eeeeee/aaaaaa.png&text=2',
          'https://dummyimage.com/300x300/eeeeee/aaaaaa.png&text=3'
        ],
        likes: 1001,
        comments: 210,
        views: 6
      }
    ]
  },

  onTabChange(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({ currentTab: index });
  },

  goPublish() {
    wx.navigateTo({ url: '/pages/circle/publish/publish' });
  },

  onShow() {
    // 读取本地存储的发布内容
    try {
      const stored = wx.getStorageSync('circle_posts') || [];
      // 将存储的内容放在前面（最新）
      const base = this.data.posts.filter(p => !p.__stored);
      const normalized = stored.map(p => ({ ...p, __stored: true }));
      this.setData({ posts: [...normalized, ...base] });
    } catch (e) {
      // 忽略错误，保持默认帖子
    }

    if (this.getTabBar && this.getTabBar()) {
      this.getTabBar().setData({ selected: 1 });
    }
  }
});
